package com.yanqu.road.server.cmd.activity.busyfarming;

import com.yanqu.road.entity.activity.busyfarming.BusyFarmingUserData;
import com.yanqu.road.entity.activity.busyfarming.enums.eBusyFarmingLandState;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.BusyFarmingProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.busyfarming.BusyFarmingModule;
import com.yanqu.road.server.gameplayer.module.activity.busyfarming.BusyFarmingPb;
import com.yanqu.road.server.manger.activity.busyfarming.BusyFarmingMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_BUSY_FARMING_STEAL_HOME_DETAIL,desc = "偷菜-果园详情")
public class BusyFarmingStealHomeDetailCmd extends AbstractCommand {

    long userId;
    long serverId;
    int csrf;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BusyFarmingProto.BusyFarmingStealHomeDetailReqMsg reqMsg = BusyFarmingProto.BusyFarmingStealHomeDetailReqMsg.parseFrom(packet.getBytes());
        userId = reqMsg.getUserId();
        serverId = reqMsg.getServerId();
        csrf = reqMsg.getCsrf();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(BusyFarmingMgr.getShowTimeActivityId() == 0){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if(!BusyFarmingMgr.activityInTime()){
            ret = GameErrorCode.E_BUSY_FARMING_ACTIVITY_NO_IN_TIME;
            return;
        }
        if(serverId == 0){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        BusyFarmingModule busyFarmingModule = player.getModule(BusyFarmingModule.class);
        BusyFarmingUserData userData = busyFarmingModule.getUserData();
        if(userData == null) {
            ret = GameErrorCode.E_BUSY_FARMING_ACTIVITY_NO_UNLOCK;
            return;
        }
        if(busyFarmingModule.GOTO_STEAL_DETAIL_CSRF_CODE == 0 || busyFarmingModule.GOTO_STEAL_DETAIL_CSRF_CODE != csrf){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        //如果是机器人
        if(userId < 10){
            BusyFarmingProto.BusyFarmingStealHomeDetailRespMsg.Builder respMsg = BusyFarmingProto.BusyFarmingStealHomeDetailRespMsg.newBuilder();
            BusyFarmingProto.CrossBusyFarmingStealDataTemp stealDataTemp = null;
            BusyFarmingProto.BusyFarmingUserDataTemp.Builder enemyBuilder = BusyFarmingProto.BusyFarmingUserDataTemp.newBuilder();
            if (busyFarmingModule.stealSearchTempList != null) {
                for (BusyFarmingProto.CrossBusyFarmingStealDataTemp dataTemp : busyFarmingModule.stealSearchTempList) {
                    if (dataTemp.getUserId() == userId) {
                        stealDataTemp = dataTemp;
                        break;
                    }
                }
                if(stealDataTemp != null) {
                    enemyBuilder.setUserId(stealDataTemp.getUserId());
                    enemyBuilder.setFruitScore(0);
                    enemyBuilder.setWateringTimes(0);
                    enemyBuilder.setBeWateringTimes(0);
                    enemyBuilder.setBeStealTimes(0);
                    enemyBuilder.setHomeGrade(stealDataTemp.getGrade());
                    enemyBuilder.setStealTimes(0);
                    enemyBuilder.setUserBaseInfo(stealDataTemp.getUserBaseInfo());
                    enemyBuilder.addAllLand(stealDataTemp.getLandList());
                }
            }

            //缓存待偷取的数据
            busyFarmingModule.stealTempData = enemyBuilder;
            busyFarmingModule.clearStealCacheData();
            respMsg.setCanSteal(true);
            respMsg.setEnemy(enemyBuilder);
            respMsg.setRet(0);
            player.sendPacket(Protocol.U_BUSY_FARMING_STEAL_HOME_DETAIL, respMsg);
            return;
        }

        if(GameServer.getInstance().getServerId() != serverId){
            //上跨服
            BusyFarmingProto.CrossBusyFarmingStealHomeDetailReqMsg.Builder reqMsg = BusyFarmingProto.CrossBusyFarmingStealHomeDetailReqMsg.newBuilder();
            reqMsg.setActivityId(BusyFarmingMgr.getShowTimeActivityId());
            reqMsg.setUserId(userId);
            reqMsg.setServerId(serverId);
            player.sendPacket(Protocol.C_BUSY_FARMING_ACTIVITY_STEAL_SEARCH_DETAIL, reqMsg);
        }else {
            BusyFarmingProto.BusyFarmingStealHomeDetailRespMsg.Builder respMsg = BusyFarmingProto.BusyFarmingStealHomeDetailRespMsg.newBuilder();
            //本服
            BusyFarmingUserData stealUserData = BusyFarmingMgr.getBusyFarmingUserData(userId);
            if(stealUserData != null){
                //是否可偷取
                boolean canSteal;
                synchronized (stealUserData) {
                    BusyFarmingMgr.calLandState(stealUserData);
                    canSteal = BusyFarmingMgr.landHasState(stealUserData, eBusyFarmingLandState.Harvest.getIntValue());
                }
                synchronized (userData){
                    BusyFarmingProto.BusyFarmingUserDataTemp.Builder stealTempData = BusyFarmingPb.getUserDataBuilder(stealUserData, true);
                    UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(userId, GameServer.getInstance().getServerId());
                    stealTempData.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo).build());
                    stealTempData.setUserId(stealUserData.getUserId());
                    respMsg.setEnemy(stealTempData);
                    if(!canSteal){
                        //归还次数
                        userData.setStealTimes(userData.getStealTimes() - 1);
                        busyFarmingModule.clearStealCacheData();

                    }else {
                        //缓存待偷取的数据
                        busyFarmingModule.stealTempData = stealTempData;
                        busyFarmingModule.clearStealCacheData();
                    }
                }
                if(!canSteal){
                    //通知被偷者
                    UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(userData.getUserId(), GameServer.getInstance().getServerId());
                    BusyFarmingMgr.dealReduction(stealUserData, stealUserData.getLandMap().get(0), 0, userData.getUserId(), userBaseInfo, 0);
                }
                respMsg.setCanSteal(canSteal);
                respMsg.setRet(0);
                player.sendPacket(Protocol.U_BUSY_FARMING_STEAL_HOME_DETAIL, respMsg);
            }
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            BusyFarmingProto.BusyFarmingStealHomeDetailRespMsg.Builder respMsg = BusyFarmingProto.BusyFarmingStealHomeDetailRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_BUSY_FARMING_STEAL_HOME_DETAIL, respMsg);
        }
    }
}
